<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
		$this->load->helper('cookie');
		$this->load->library('Session');
		$this->cklogin();
		$this->load->model('Manager_model','manager_model');
    }

	public function index()
	{
		$this->load->view('admin/template.html');
	}

	//管理员
	public function manager(){
		$data['siderid']=3;
		$query = $this->manager_model->getall();
		$data['manager']=$query->result();
		$data['nums'] = $query->num_rows();
		$this->load->view('admin/manager.html',$data);
	}

	//添加管理员表单
	public function manager_form(){
		$data['siderid']=3;
		$this->load->view('admin/addmanager.html',$data);
	}
	
	//添加管理员
	public function addmanager(){
		$post = $this->input->post();
		$bool = $this->manager_model->addone($post);
		if($bool){
			echo "添加成功，<a href='".site_url('admin/manager_form')."'>点击继续</a>";
		}else{
			echo "添加失败!";
		}
	}

	//停用启用
	public function status($id){
		$bool = $this->manager_model->setstatus($id);
		$this->manager();
	}

	//通用检测登录
	function cklogin(){
		$userid = $this->session->userid;
		if(!$userid){
			redirect('login');
		}
	}

	//个人中心
	public function profile(){
		$id = $this->uri->segment(3,0);
		if($id==0){
			$id = $this->session->userid;	
		}
		$data['user'] = $this->manager_model->getsta($id);
		if(!$data['user']){
			echo "<script>alert('无此用户！');location.href=history.go(-1);</script>";	
		}
		$data['userpic'] =$this->getuserpic($id);
		$this->load->model('article_model');
		$data['arts'] = $this->article_model->getuidart($id)->result();
		$data['num'] = $this->article_model->getuidart($id)->num_rows();
		$data['cate'] = $this->article_model->getcate()->result();
		$this->load->view('admin/profile.html',$data);
	}

	//个人信息编辑表单
	public function editprofile(){
		$id = $this->session->userid;
		$data['user'] = $this->manager_model->getsta($id);
		$data['userpic'] =$this->getuserpic($id);
		$this->load->view('admin/profile_form.html',$data);
	}

	//用户头像上传
	public function userimgedit(){
		$this->config->load('blog_settings');
		$config['upload_path']      = $this->config->item('userpicpath');
        $config['allowed_types']    = 'jpg|png';
		$config['encrypt_name']        =true;
        $config['max_size']     = 100;
        $config['max_width']        = 500;
        $config['max_height']       = 500;

        $this->load->library('upload', $config);

		 if ( ! $this->upload->do_upload('userfile')){
            $error = $this->upload->display_errors();

			echo $error;
        }
        else{
			$id = $this->session->userid;
			$userpicname = $this->upload->data('file_name');
            $res = $this->manager_model->setuserpic($userpicname,$id);
			if($res){
				echo "<script>alert('设置成功！');location.href=history.go(-1);</script>";
			}
        }
	}
	
	//修改头像url
	public function userimgurl(){
		$url = $this->input->post('imgurl');
		$reg = '/((http|https):\/\/)+(\w+\.)+(\w+)[\w\/\.\-]*(jpg|png)/';
		preg_match($reg,$url,$result);
		if($result){
			$id = $this->session->userid;
			$res = $this->manager_model->setuserpic($url,$id);
			if($res){
				echo "<script>alert('设置成功！');location.href=history.go(-1);</script>";
			}
		}else{
			echo "<script>alert('不是有效的图片地址！');location.href=history.go(-1);</script>";
		}
	}

	//获取用户头像
	public function getuserpic($id){
		$this->config->load('blog_settings');
		if(!isset($id)){
			$id = $this->session->userid;	
		}
		$userpic =  $this->manager_model->getuserpic($id)->userpic;
		if($userpic){
			$reg = '/((http|https):\/\/)+(\w+\.)+(\w+)[\w\/\.\-]*(jpg|png)/';
			preg_match($reg,$userpic,$result);
			if($result){
				return 	$userpic;
			}else{
				return base_url().$this->config->item('userpicpath').$userpic;
			}
		}else{
			return base_url('resource/adminlte/dist/img/avatar5.png');
		}
	}

	//修改用户资料
	public function userinfoedit(){
		$data = $this->input->post();
		$data['id'] = $this->session->userid;
		$res = $this->manager_model->setuserinfo($data);
		if($res){
				echo "<script>alert('设置成功！');location.href=history.go(-1);</script>";
		}
	}

	//网站设置
	public function sysinfo(){
		$data['info'] = $this->manager_model->getsysinfo();
		$data['siderid']=1;
		$this->load->view('admin/sysinfo_form.html',$data);
	}

	//网站设置处理
	public function setsysinfo(){
		if(!ckuserlevel()){
			echo "<script>alert('你无此权限');location.href=history.go(-1);</script>";
			exit;
		}
		$data = $this->input->post();
		$res = $this->manager_model->setsysinfo($data);
		if($res){
			echo "<script>alert('设置成功！');location.href=history.go(-1);</script>";
		}
	}

	//网站功能开关处理
	public function setonoff(){
		if(!ckuserlevel()){
			echo "";
			exit;
		}
		$info = $this->input->post('switchname');
		$res = $this->manager_model->setonoff($info);
		if($res){
			echo "1";
		}
	}

	public function test(){
		echo "<br/>".$_SERVER['SERVER_SOFTWARE'];
		echo "<br/>php:".PHP_VERSION;
		echo "<br/>ip:".$_SERVER['REMOTE_ADDR'];
		echo "<br/>apache:".apache_get_version();
		echo "<br/>mysql:".$this->db->platform();
		echo "<br/>mysql:".$this->db->version();
		echo "<br/>".php_uname();
		echo "<br/>".php_sapi_name();
		echo "<br/>".GetHostByName($_SERVER['SERVER_NAME']);
		echo "<br/>".$_SERVER['REMOTE_ADDR'];
	}
}
